#include "messageeditarea.h"
#include "mainwidget.h"
#include "model/datacenter.h"
#include "toast.h"

MessageEditArea::MessageEditArea(QWidget *parent) : QWidget{parent}
{
    // 1.设置必要的属性
    this->setFixedHeight(200);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 2. 创建垂直方向的布局管理器
    QVBoxLayout *vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(10, 0, 10, 10);
    this->setLayout(vlayout);

    // 3. 创建水平方向的布局管理器
    QHBoxLayout *hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(10, 0, 0, 0);
    hlayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    vlayout->addLayout(hlayout);

    // 4. 把上方的四个按钮，创建好添加到水平布局管理器中
    QString btnStyle = "QPushButton { background-color: rgb(245, 245, 245); "
                       "border: none; } QPushButton:pressed { "
                       "background-color: rgb(255, 255, 255); }";
    QSize btnSize(35, 35);
    QSize iconSize(25, 25);

    _sendImageBtn = new QPushButton();
    _sendImageBtn->setFixedSize(btnSize);
    _sendImageBtn->setIconSize(iconSize);
    _sendImageBtn->setIcon(QIcon(":/resource/image/image.png"));
    _sendImageBtn->setStyleSheet(btnStyle);
    hlayout->addWidget(_sendImageBtn);

    _sendFileBtn = new QPushButton();
    _sendFileBtn->setFixedSize(btnSize);
    _sendFileBtn->setIconSize(iconSize);
    _sendFileBtn->setIcon(QIcon(":/resource/image/file.png"));
    _sendFileBtn->setStyleSheet(btnStyle);
    hlayout->addWidget(_sendFileBtn);

    _sendSpeechBtn = new QPushButton();
    _sendSpeechBtn->setFixedSize(btnSize);
    _sendSpeechBtn->setIconSize(iconSize);
    _sendSpeechBtn->setIcon(QIcon(":/resource/image/sound.png"));
    _sendSpeechBtn->setStyleSheet(btnStyle);
    hlayout->addWidget(_sendSpeechBtn);

    _showHistoryBtn = new QPushButton();
    _showHistoryBtn->setFixedSize(btnSize);

    _showHistoryBtn->setIconSize(iconSize);
    _showHistoryBtn->setIcon(QIcon(":/resource/image/history.png"));
    _showHistoryBtn->setStyleSheet(btnStyle);
    hlayout->addWidget(_showHistoryBtn);

    // 5. 添加多行编辑框
    _textEdit = new QPlainTextEdit();
    _textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    _textEdit->setStyleSheet("QPlainTextEdit { border: none; background-color: "
                             "transparent; font-size: 14px; padding: 10px; }");
    _textEdit->verticalScrollBar()->setStyleSheet(
        "ScrollBar:vertical { width: 2px; background-color: rgb(45, 45, 45); }");
    vlayout->addWidget(_textEdit);

    // 6. 添加“录制中” 提示 QLabel
    _tipLabel = new QLabel();
    _tipLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    _tipLabel->setText("录制中...");
    _tipLabel->setAlignment(Qt::AlignCenter);
    _tipLabel->setFont(QFont("微软雅黑", 24, 600));
    vlayout->addWidget(_tipLabel);
    _tipLabel->hide();

    // 7. 添加发送文本消息的按钮
    _sendTextBtn = new QPushButton();
    _sendTextBtn->setText("发送");
    _sendTextBtn->setFixedSize(120, 40);
    QString style = "QPushButton { font-size: 16px; color: rgb(7, 193, 96); "
                    "border: none; background-color: rgb(233, "
                    "233, 233); border-radius: 10px; } ";
    style += "QPushButton:hover { background-color: rgb(210, 210, 210); }";
    style += "QPushButton:pressed { background-color: rgb(190, 190, 190); }";
    _sendTextBtn->setStyleSheet(style);
    vlayout->addWidget(_sendTextBtn, 0, Qt::AlignRight | Qt::AlignVCenter);

    // 8. 初始化槽函数
    initSignalSlot();
}
void MessageEditArea::initSignalSlot()
{
    model::DataCenter *dataCenter = model::DataCenter::getInstance();

    // 1. 关联 显示历史信息窗口 信号槽
    connect(_showHistoryBtn, &QPushButton::clicked, this,
            [=]()
            {
                HistoryMessageWidget *historyMessageWidget = new HistoryMessageWidget(this);
                historyMessageWidget->exec();
            });

    // 2. 关联发送 文本/图片/语音 和 收到消息 信号槽
    // 发送文本消息
    connect(_sendTextBtn, &QPushButton::clicked, this, &MessageEditArea::sendTextMessage);
    connect(dataCenter, &model::DataCenter::sendMessageDone, this, &MessageEditArea::addSelfMessage);

    // // 收到消息
    // connect(_sendImageBtn, &QPushButton::clicked, this, &MessageEditArea::clickSendImageBtn);

    // // 发送图片消息
    // connect(_sendFileBtn, &QPushButton::clicked, this, &MessageEditArea::clickSendFileBtn);

    // // 发送语音消息
    // connect(_sendSpeechBtn, &QPushButton::pressed, this, &MessageEditArea::soundRecordPreesed);
    // connect(_sendSpeechBtn, &QPushButton::released, this, &MessageEditArea::soundRecordReleased);
    // SoundRecorder *soudRecorder = SoundRecorder::getInstance();
    // connect(soudRecorder, &SoundRecorder::soudRecordDone, this, &MessageEditArea::sendSpeech);
}

void MessageEditArea::sendTextMessage()
{
    // 1. 先确认当前是否已经有会话被选中了，如果没有选中则直接返回
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    if (dataCenter->getCurrentChatSessionId().isEmpty())
    {
        LOG() << "当前未选中任何会话，不会发送消息！";
        Toast::showMessage("当前未选中会话，不发送任何消息！");
        return;
    }

    // 2. 获取到输入框的内容，看输入框里是否有内容，如果没有内容直接返回
    const QString &content = _textEdit->toPlainText().trimmed(); // 禁止发送空白消息，使用 trimmed 删除两边的空格
    if (content.isEmpty())
    {
        LOG() << "输入框为空";
        return;
    }

    // 3. 清空输入框已有内容
    _textEdit->setPlainText("");

    // 4. 通过网络发送数据给服务器
    dataCenter->sendTextMessageAsync(dataCenter->getCurrentChatSessionId(), content);
}

// 自己发送消息处理
void MessageEditArea::addSelfMessage(model::MessageType messageType, const QByteArray &content,
                                     const QString &extraInfo)
{
    model::DataCenter *dataCenter       = model::DataCenter::getInstance();
    const QString &currentChatSessionId = dataCenter->getCurrentChatSessionId();

    // 1. 构造出一个消息对象
    model::Message message =
        model::Message::makeMessage(messageType, currentChatSessionId, *dataCenter->getMyself(), content, extraInfo);
    dataCenter->addMessage(message);

    // 2. 把这个新消息显示到消息区
    MainWidget *mainWidget           = MainWidget::getInstance();
    MessageShowArea *messageShowArea = mainWidget->getMessageShowArea();
    messageShowArea->addMessage(false, message);

    // 3. 控制消息显示区，滚动条滚到末尾
    messageShowArea->scrollToEnd();

    // 4. 发送信号，通知会话列表，更新最后一条消息
    emit dataCenter->updateLastMessage(currentChatSessionId);
}

void MessageEditArea::addOtherMessage(const model::Message &message)
{
    //1. 通过主界面，拿到消息展示区
    MainWidget *mainWidget = MainWidget::getInstance();
    MessageShowArea *messageShowArea = mainWidget->getMessageShowArea();

    // 2. 把收到的新消息添加到消息展示区
    messageShowArea->addMessage(true,message);

    //3. 控制消息展示区的滚动条，把窗口滚动到末尾
    messageShowArea->scrollToEnd();

    //4. 提示一个收到消息
    Toast::showMessage("收到新消息！");
}
